package com.yu.footballpromotionliveadmin.dao.impl;

import com.yu.footballpromotionliveadmin.dao.CompetitionRecordDao;
import com.yu.footballpromotionliveadmin.mapper.CompetitionRecordMapper;
import com.yu.footballpromotionliveadmin.page.PageInfo;
import com.yu.footballpromotionliveadmin.pojo.CompetitionRecord;
import com.yu.footballpromotionliveadmin.util.GenNumTools;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Repository
public class CompetitionRecordDaoImpl implements CompetitionRecordDao {

    @Resource
    CompetitionRecordMapper competitionRecordMapper;

    @Override
    public List<CompetitionRecord> getAllCompetitionRecors() throws Exception {
        return competitionRecordMapper.selectByExample(null);
    }

    @Override
    public CompetitionRecord getCompetitionRecorByUno(String crno) throws Exception {
        return competitionRecordMapper.selectByPrimaryKey(crno);
    }

    @Override
    public List<CompetitionRecord> getCompetitionRecorsByPage(PageInfo pageInfo) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("currPage",pageInfo.getCurrPage());
        map.put("pageSize",pageInfo.getPageSize());
        int itemCount =(int) competitionRecordMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return competitionRecordMapper.getCompetitionRecordByPage(map);
    }

    @Override
    public Integer deleteCompetitionRecor(String crno) throws Exception {
        return competitionRecordMapper.deleteByKey(crno);
    }

    @Override
    public Integer updateCompetitionRecor(CompetitionRecord competitionRecord) throws Exception {
        return competitionRecordMapper.updateByPrimaryKey(competitionRecord);
    }

    @Override
    public Integer addCompetitionRecorByUno(CompetitionRecord competitionRecord) throws Exception {
        competitionRecord.setCrno(initId());
        return competitionRecordMapper.insert(competitionRecord);
    }

    @Override
    public CompetitionRecord getCompetitionRecorsWithOrders(String crno) throws Exception {
        return null;
    }

    @Override
    public CompetitionRecord getAccountByAccountName(String crno) throws Exception {
        return null;
    }

    public String initId() {
        String maxUno = competitionRecordMapper.getMaxUno();
        maxUno =  Optional.ofNullable(maxUno).orElse("C0000");
        return GenNumTools.initId("C", 4, maxUno);
    }
}
